﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.Model;
using Wsitech.BLL;
using Wsitech.Utility;
using System.Collections;

public partial class Backend_VideoList : BackendPageBase
{
    protected readonly string parentSitemapid = HttpContext.Current.Request["sitemapId"] ?? "";
    /// <summary>
    /// 栏目名称
    /// </summary>
    public string SiteMapName
    {
        get
        {
            if (!string.IsNullOrEmpty(parentSitemapid))
            {
                SiteMapInfo map = SiteMapInfoBLL.Instance.GetDetail(parentSitemapid);
                if (map != null)
                {
                    return map.SitemapName;
                }
                else
                {
                    return "找不到栏目";
                }
            }
            else
            {
                return "";
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindSitemap();
            bindVideoInfo();
        }
    }

    private void bindSitemap()
    {
        ddlSitemap.DataSource = SiteMapInfoBLL.Instance.GetSiteMapByParentId(parentSitemapid);
        ddlSitemap.DataTextField = "SitemapName";
        ddlSitemap.DataValueField = "SitemapId";
        ddlSitemap.DataBind();

        ddlSitemap.Items.Insert(0, new ListItem("==全部==", ""));
    }

    private void bindVideoInfo()
    {
        Hashtable ht = new Hashtable();
        if (Request["title"] != null)
        {
            ht.Add("title", Request["title"] ?? "");
        }
        IList<TextInfo> list = new List<TextInfo>();
        int count = 0;
        if (Request["classid"] != null)
        {
            string sitemapid = Request["classid"] ?? "";
            list = TextInfoBLL.Instance.GetPagedListBySiteMapID(sitemapid, ht, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref count);
        }
        else
        {
            list = TextInfoBLL.Instance.GetPagedListByParentSiteMapID(parentSitemapid, ht, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref count);
        }
        repMain.DataSource = list;
        repMain.DataBind();
        AspNetPager1.RecordCount = count;
    }


    protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton lbtnEnable = (LinkButton)e.Item.FindControl("lbtnEnable");
        LinkButton lbtnDisable = (LinkButton)e.Item.FindControl("lbtnDisable");

        //显示和隐藏启用与禁用按钮
        TextInfo info = e.Item.DataItem as TextInfo;
        if (info != null)
        {
            if (info.State == (int)UtilityEnum.State.Enable)
            {
                lbtnEnable.Visible = false;
            }
            if (info.State == (int)UtilityEnum.State.Disable)
            {
                lbtnDisable.Visible = false;
            }
        }
    }

    protected void repMain_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string tid = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "Enable":
                TextInfoBLL.Instance.ChangeState(tid, UtilityEnum.State.Enable);
                break;
            case "Disable":
                TextInfoBLL.Instance.ChangeState(tid, UtilityEnum.State.Disable);
                break;
            default:
                break;
        }
        Response.Redirect(Request.Url.ToString());
    }


    //分页
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindVideoInfo();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string redirectUrl = string.Format("{0}?sitemapid={1}", Request.Path, parentSitemapid);
        if (txtTitle.Text.Trim() != "")
        {
            redirectUrl += "&title=" + txtTitle.Text.Trim();
        }

        if (ddlSitemap.SelectedIndex != 0)
        {
            redirectUrl += "&classid=" + ddlSitemap.SelectedValue;
        }

        Response.Redirect(redirectUrl);
    }
}